package com.fastCms.business.bim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastCms.business.frame.constant.CommonConst;
import com.fastCms.business.bim.daos.ReportDataMapper;
import com.fastCms.business.bim.enums.ReportTypeEnum;
import com.fastCms.business.bim.pojo.entity.ReportData;
import com.fastCms.business.bim.service.ReportDataService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 区域管理 服务实现类
 * </p>
 *
 * @author liusq
 * @since 2021-04-05
 */
@Service
public class ReportDataServiceImpl extends ServiceImpl<ReportDataMapper, ReportData> implements ReportDataService {

    @Override
    public ReportData findCompany() {
        QueryWrapper<ReportData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        queryWrapper.eq("report_type", ReportTypeEnum.COMPANY.getType());
        return getOne(queryWrapper);
    }

    @Override
    public List<ReportData> findValidAll(String reportType, Integer limit) {
        QueryWrapper<ReportData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        if (Objects.nonNull(limit)) {
            queryWrapper.last(" limit " + limit);
        }
        queryWrapper.eq("report_type", reportType);
        queryWrapper.orderByDesc("import_time");
        return list(queryWrapper);
    }

    @Override
    public void deleteBySerialNo(String serialNo) {
        ReportData reportData = findBySerialNo(serialNo);
        if (null != reportData) {
            reportData.setState(CommonConst.STATE.INVALID);
        }
        saveOrUpdate(reportData);
    }

    public ReportData findBySerialNo(String serialNo) {
        UpdateWrapper<ReportData> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("serial_no", serialNo);
        return getOne(queryWrapper);
    }

}
